package sourceagile.server.gitFileRepository;

import java.io.File;

import org.eclipse.jgit.api.Git;

public class GetGitClassFile {

	public static void jsonClassDocument() {

		// RepositoryBuilder builder = new RepositoryBuilder();

		File file = new File(System.getProperty("java.io.tmpdir") + "Jsonpfy");

		Git git3 = Git.cloneRepository()
				.setURI("https://allineo@github.com/allineo/Jsonpfy.git")
				.setDirectory(file).call();

		File[] files = git3.getRepository().getWorkTree().listFiles();

		for (File file2 : files) {

			System.out.println(file2);
		}

		/*
		 * // new File(DirectoryUtils.getUUIDPath("/tmp", ""), (new //
		 * URIish("")).getHumanishName())
		 * 
		 * Repository repository = new RepositoryBuilder().setGitDir( new
		 * File("https://github.com/allineo/Jsonpfy.git")).build();
		 * 
		 * 
		 * GitFactory theGitFactory = (GitFactory) EPackage.Registry.INSTANCE
		 * .getEFactory("http://unicase.org/changetracking/git");
		 * 
		 * Repository remoteRepo = GitFactory.eINSTANCE.createGitRepository();
		 * remoteRepo.setName("Git-Hub"); String url; url =
		 * "https://github.com/gexicide/testor.git"; // url =
		 * "git@github.com:gexicide/testor.git";
		 * 
		 * remoteRepo.setUrl(url); Map<String, Ref> refs = r.getAllRefs();
		 * CredentialsProvider provider = new SayYesCredentialsProvider(
		 * "gexicide", "Fuck4git");
		 * 
		 * 
		 * Repository fileRepository = builder.setGitDir(new File("gitTest"))
		 * .readEnvironment() // scan environment GIT_* variables .findGitDir()
		 * // scan up the file system tree .build();
		 * 
		 * File repoDir = new File("test-git");
		 * 
		 * // Repository fileRepository = new FileRepository("gitTest");
		 * 
		 * Repository fileRepository = new FileRepository(
		 * "https://github.com/allineo/Jsonpfy"); //
		 * "git@github.com:allineo/Jsonpfy.git");
		 * 
		 * // FileRepository fileRepository = new FileRepository(new //
		 * File(".git", // Constants.DOT_GIT));
		 * 
		 * // fileRepository.create(false);
		 * 
		 * Config config = fileRepository.getConfig();
		 * 
		 * Config config2 = repository.getConfig();
		 * 
		 * // URIish uri = new //
		 * URIish("https://github.com/allineo/Jsonpfy.git"); URIish uri = new
		 * URIish("https://github.com/allineo/Jsonpfy.git");
		 * uri.setUser("allineo"); uri.setPass("luizinho0820"); //
		 * CredentialsProvider credentialsProvider = new //
		 * UsernamePasswordCredentialsProvider( // uri.getUser(),
		 * uri.getPass()); RemoteConfig remoteConfig = new RemoteConfig(config,
		 * Constants.HEAD);
		 * 
		 * RemoteConfig remoteConfig2 = new RemoteConfig(config2,
		 * Constants.HEAD);
		 * 
		 * remoteConfig.addURI(uri);
		 * 
		 * Set<String> remotes = config.getSubsections("remote");
		 * 
		 * String branch = fileRepository.getBranch();
		 * 
		 * Map<String, Ref> tags = fileRepository.getTags(); for (String key :
		 * tags.keySet()) { System.out.println(key + ":" +
		 * tags.get(key).toString()); }
		 * 
		 * ObjectId lastCommitId = fileRepository.resolve(Constants.HEAD);
		 * 
		 * String branch2 = repository.getBranch();
		 * 
		 * Ref HEAD = fileRepository.getRef("refs/heads/master");
		 * 
		 * System.out.println(lastCommitId);
		 * 
		 * Git git = new Git(fileRepository);
		 * 
		 * Git git2 = new Git(repository);
		 * 
		 * RevWalk revWalk = new RevWalk(fileRepository); RevCommit commit =
		 * revWalk.parseCommit(lastCommitId); // and using commit's tree find
		 * the path RevTree tree = commit.getTree(); TreeWalk treeWalk = new
		 * TreeWalk(fileRepository); treeWalk.addTree(tree); //
		 * treeWalk.setFilter(PathFilter.create(path)); if (!treeWalk.next()) {
		 * // return null; } ObjectId objectId = treeWalk.getObjectId(0);
		 * ObjectLoader loader = fileRepository.open(objectId);
		 */

	}

	// Deletes all files and subdirectories under dir.
	// Returns true if all deletions were successful.
	// If a deletion fails, the method stops attempting to delete and returns
	// false.
	public static boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {

					System.out.println(dir);
					return false;

				}
			}
		}

		// The directory is now empty so delete it
		return dir.delete();
	}
}
